import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.ts';
import { Viewer } from "../Viewer";
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.ts';
import * as THREE from 'three';
import { BaseModel } from '../BaseModel';

type LoadModelCallbackFn<T = any> = (arg: T) => any;

export class ModelLoder {
    protected viewer: Viewer
    private gLTFLoader: GLTFLoader
    private dracoLoader: DRACOLoader;

    constructor(viewer: Viewer, dracolPath: string = '/draco/') {
        this.viewer = viewer
        this.gLTFLoader = new GLTFLoader()
        this.dracoLoader = new DRACOLoader()

        this.dracoLoader.setDecoderPath(dracolPath)
        this.gLTFLoader.setDRACOLoader(this.dracoLoader)
    }

    // 模型加载到场景
    public loadModel(url: string, callback: LoadModelCallbackFn<BaseModel>) {
        this.gLTFLoader.load(url, (gltf) => {
            callback(gltf.scene)
        })
    }
}